package com.bw.finance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.bw.finance.conf.ResponseResult;
import com.bw.finance.eneity.TbUserRole;
import com.bw.finance.service.ITbUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhengqian
 * @since 2020-05-09
 */
@RestController
@RequestMapping("/userRole")
public class TbUserRoleController {
    @Autowired
    private ITbUserRoleService userRoleService;
    @PostMapping("/save")
    public ResponseResult save(Long userid, Long[] roleIds){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            userRoleService.add(userid,roleIds);
            result.setMessage("保存成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        return result;
    }
    @GetMapping("/getroleid/{id}")
    public ResponseResult save(@PathVariable long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("USER_ID",id);
            List<TbUserRole> list = userRoleService.list(queryWrapper);
            List<Long> roleIds=new ArrayList<>();
            for (TbUserRole t:list) {
                roleIds.add(t.getRoleId());
            }
            result.setResult(roleIds);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        return result;
    }

}
